/**
 * Copyright ©2016-2020 tianyu education Corporation, All Rights Reserved
 * http://www.whty.com.cn/
 */
package profile.hujia.profile.elasticsearch.study.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * class info
 *
 * @author hujia
 * @date 2019-09-04 10:13
 */
@Service
@Slf4j
public class IndexServiceImpl implements IndexService {

    @Autowired
    private RestHighLevelClient restClient;

    @Autowired
    ObjectMapper jackson;

    @Override
    public void createIndex(String name) throws IOException {
        CreateIndexRequest request = new CreateIndexRequest(name);

        request.settings(Settings.builder().put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)
                .put("analysis.analyzer.default.tokenizer", "ik_smart"));

//        Map<String, Object> resource = new HashMap<>();
//        resource.put("type", "text");
//        Map<String, Object> properties = new HashMap<>();
//        properties.put("resource", resource);
//        Map<String, Object> mapping = new HashMap<>();
//        mapping.put("properties", properties);
//        request.mapping( mapping);

        CreateIndexResponse createIndexResponse = restClient.indices()
                    .create(request, RequestOptions.DEFAULT);
//        boolean acknowledged = createIndexResponse.isAcknowledged();
//        boolean shardsAcknowledged = createIndexResponse
//                .isShardsAcknowledged();
    }

    @Override
    public void deleteIndex(String name) throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest(name);
        restClient.indices().delete(request, RequestOptions.DEFAULT);
    }
}
